<%--
  Created by IntelliJ IDEA.
  User: aa
  Date: 2019/07/17
  Time: 15:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html lang="ch-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <link rel="stylesheet" href="${APP_PATH}/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="${APP_PATH}/css/font-awesome.min.css">
    <link rel="stylesheet" href="${APP_PATH}/css/main.css">
    <style>
        .tree li {
            list-style-type: none;
            cursor: pointer;
        }

        table tbody tr:nth-child(odd) {
            background: #F4F4F4;
        }

        table tbody td:nth-child(even) {
            color: #C00;
        }
    </style>
</head>

<body>

<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
    <div class="container-fluid">
        <div class="navbar-header">
            <div><a class="navbar-brand" style="font-size:32px;" href="#">众筹平台 - 用户维护</a></div>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
            <%--头部导航栏--%>
            <jsp:include page="/WEB-INF/jsp/common/headShow.jsp"></jsp:include>
            <form class="navbar-form navbar-right">
                <input type="text" class="form-control" placeholder="Search...">
            </form>
        </div>
    </div>
</nav>

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
            <div class="tree">
                <%--左方功能树导航栏,静态包含--%>
                <%@ include file="/WEB-INF/jsp/common/menu.jsp" %>
            </div>
        </div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                </div>
                <div class="panel-body">
                    <form class="form-inline" role="form" style="float:left;">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input id="queryText" class="form-control has-success" type="text"
                                       placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button id="queryBtn" type="button" class="btn btn-warning"><i
                                class="glyphicon glyphicon-search"></i> 查询
                        </button>
                    </form>
                    <button id="deleteAllBtn" type="button" class="btn btn-danger" style="float:right;margin-left:10px;"><i
                            class=" glyphicon glyphicon-remove"></i> 删除
                    </button>
                    <button type="button" class="btn btn-primary" style="float:right;"
                            onclick="window.location.href='${APP_PATH}/user/toAdd.htm'"><i
                            class="glyphicon glyphicon-plus"></i> 新增
                    </button>
                    <br>
                    <hr style="clear:both;">
                    <div class="table-responsive">
                        <table class="table  table-bordered" id="table_table">
                            <thead>
                            <tr>
                                <th width="30">#</th>
                                <th width="30"><input id="checkboxAll" type="checkbox"></th>
                                <th>账号</th>
                                <th>名称</th>
                                <th>邮箱地址</th>
                                <th width="100">操作</th>
                            </tr>
                            </thead>
                            <tbody>

                            </tbody>
                            <tfoot>
                            <tr>
                                <td colspan="6" align="center">

                                </td>
                            </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="${APP_PATH}/jquery/jquery-2.1.1.min.js"></script>
<script src="${APP_PATH}/bootstrap/js/bootstrap.min.js"></script>
<script src="${APP_PATH}/script/docs.min.js"></script>
<script type="text/javascript" src="${APP_PATH}/jquery/layer/layer.js"></script>
<%--调用菜单点击js--%>
<script src="${APP_PATH}/script/menu.js"></script>
<script type="text/javascript">
    $(function () {
        $(".list-group-item").click(function () {
            if ($(this).find("ul")) {
                $(this).toggleClass("tree-closed");
                if ($(this).hasClass("tree-closed")) {
                    $("ul", this).hide("fast");
                } else {
                    $("ul", this).show("fast");
                }
            }
        });
        toPageNume(1);
        showMenu();
    });

    $('#userMain').click(function () {
        toPageNume(1);
    });

    //为服务器传递参数用的
    var jsonObje = {};


    function toPageNume(num) {
        jsonObje.pageNum = num;
        $.ajax({
            url: "${APP_PATH}/user/index.do",
            data: jsonObje,
            type: "POST",
            success: function (result) {
                //解析并显示员工数据
                build_table_body(result);
                //解析并显示分页按钮
                build_page_btn(result);
            }
        });
    }

    //查询按钮操作
    $('#queryBtn').click(function () {
        var queryText = $('#queryText').val();
        jsonObje.queryText = queryText;
        toPageNume(1);
    });

    //添加分页导航条
    function build_page_btn(result) {
        $("#table_table tfoot tr td").empty();
        var ul = $("<ul></ul>").addClass("pagination")
        var firstPageLi = $("<li></li>").append($("<a></a>").append("首页").attr("href", "#"));
        var prePageLi = $("<li></li>").append($("<a></a>").append("&laquo;"));
        if (result.extend.pageInfo.hasPreviousPage == false) {
            firstPageLi.addClass("disabled");
            prePageLi.addClass("disabled");
        }
        firstPageLi.click(function () {
            toPageNume(1);
        })
        prePageLi.click(function () {
            if (result.extend.pageInfo.hasPreviousPage) {
                toPageNume(result.extend.pageInfo.pageNum - 1);
            }
        });

        var lastPageLi = $("<li></li>").append($("<a></a>").append("末页").attr("href", "#"));
        var nextPageLi = $("<li></li>").append($("<a></a>").append("&raquo;"));
        if (result.extend.pageInfo.hasNextPage == false) {
            lastPageLi.addClass("disabled");
            nextPageLi.addClass("disabled");
        }
        lastPageLi.click(function () {
            toPageNume(result.extend.pageInfo.pages);
        })
        nextPageLi.click(function () {
            if (result.extend.pageInfo.hasNextPage) {
                toPageNume(result.extend.pageInfo.pageNum + 1)
            }
        })
        ul.append(firstPageLi).append(prePageLi);

        $.each(result.extend.pageInfo.navigatepageNums, function (index, item) {
            var numLi = $("<li></li>").append($("<a></a>").append(item));
            if (result.extend.pageInfo.pageNum == item) {
                numLi.addClass("active")
            }
            numLi.click(function () {
                toPageNume(item);
            });
            ul.append(numLi);
        });
        ul.append(lastPageLi).append(nextPageLi);

        var navEle = $("<nav></nav>").append(ul);
        var page_info = $("<div></div>").append("当前第" + result.extend.pageInfo.pageNum + "页;&emsp;" +
            "总" + result.extend.pageInfo.pages + "页;&emsp;共" + result.extend.pageInfo.total + "条记录");

        page_info.appendTo("#table_table tfoot tr td");
        navEle.appendTo("#table_table tfoot tr td");
    }

    //添加页面数据
    function build_table_body(result) {
        $("#table_table tbody").empty();
        var users = result.extend.pageInfo.list;
        $.each(users, function (index, item) {
            var userId = $("<td></td>").append(index + 1);
            var checkBoxInput = $("<input></input>").attr({"type":"checkbox","id":item.id});
            var checkbox = $("<td></td>").append(checkBoxInput);
            var userloginacct = $("<td></td>").append(item.loginacct);
            var username = $("<td></td>").append(item.username);
            var useremail = $("<td></td>").append(item.email);

        // <button type="button" class="btn btn-success btn-xs"><i class=" glyphicon glyphicon-check"></i></button>
            var addRoleBtn = $("<button></button>").addClass("btn btn-success btn-xs")
                .append($("<i></i>").addClass("glyphicon glyphicon-check"));
            //给添加角色按钮点击事件
            addRoleBtn.click(function () {
                window.location.href = "${APP_PATH}/user/toAssignRole.htm?userId=" + item.id;
            });

            // <button type="button" class="btn btn-primary btn-xs"><i class=" glyphicon glyphicon-pencil"></i></button>
            var editBtn = $("<button></button>").addClass("btn btn-primary btn-xs")
                .append($("<i></i>").addClass("glyphicon glyphicon-pencil"));

            //给编辑按钮添加点击事件
            editBtn.click(function () {
                window.location.href = "${APP_PATH}/user/toEdit.htm?userId=" + item.id;
            });
            // <button type="button" class="btn btn-danger btn-xs"><i class=" glyphicon glyphicon-remove"></i></button>
            var deleteBtn = $("<button></button>").addClass("btn btn-danger btn-xs")
                .append($("<i></i>").addClass("glyphicon glyphicon-remove"));
            //删除按钮

            deleteBtn.click(function () {
                layer.confirm('您是否删除'+item.loginacct, {
                    btn: ['取消', '确定']
                }, function(index){
                    layer.close(index);
                }, function(index){
                    //alert(item.id);
                    deleteUser(item.id);
                });
            });

            var btn = $("<td></td>").append(addRoleBtn).append(" ").append(editBtn).append(" ").append(deleteBtn);

            $("<tr></tr>").append(userId).append(checkbox).append(userloginacct)
                .append(username).append(useremail).append(btn).appendTo("#table_table tbody");
        });
    }

    function deleteUser(id) {
        $.ajax({
            type: "POST",
            data: {"userId": id},
            url: "${APP_PATH}/user/deleteUser.do",
            beforeSend: function () {
                return true;
            },
            success: function (reslut) {
                if (reslut.code == 100) {
                    layer.msg("删除成功");
                    window.location.href = "${APP_PATH}/user/toIndex.htm";
                } else {
                    layer.msg("无法删除");
                }
            },
            error: function () {
                layer.msg("未知错误");
            }
        });
    }
    //批量删除
    $("#deleteAllBtn").click(function () {
        var selectCheckBox = $("#table_table tbody tr td input:checked");
        if(selectCheckBox.length == 0){
            layer.msg("无法删除");
            return false;
        }
        var idStr = "";
        $.each(selectCheckBox,function (index, item) {
            if(index != 0){
                idStr += '&';
            }
            idStr += "id="+item.id;
        })
        layer.confirm('您是否删除这些用户吗', {
            btn: ['取消', '确定']
        }, function(index){
            layer.close(index);
        }, function(index){
            //alert(item.id);
            $.ajax({
                type: "POST",
                data: idStr,
                url: "${APP_PATH}/user/deleteBatch.do",
                beforeSend: function () {

                    return true;
                },
                success: function (reslut) {
                    if (reslut.code == 100) {
                        layer.msg("删除成功");
                        window.location.href = "${APP_PATH}/user/toIndex.htm";
                    } else {
                        layer.msg("无法删除");
                    }
                },
                error: function () {
                    layer.msg("未知错误");
                }
            });
        });
    });


    //全选
    $("#checkboxAll").click(function () {
        var check = this.checked;
        $("#table_table tbody tr td input[type='checkbox']").prop("checked",check);
    });



    $("tbody .btn-success").click(function () {
        window.location.href = "assignRole.html";
    });
    $("tbody .btn-primary").click(function () {
        window.location.href = "edit.html";
    });


</script>
</body>
</html>
